package org.gap.jseed.injection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.gap.jseed.Injector;
import org.gap.jseed.reflect.MethodIterator;

public abstract class BaseInjector implements Injector {

	@Override
	public void inject(ClassWriter writer, Class<?> source) {
		for (Method each : new MethodIterator(source)) {
			handleMethodAnnotations(writer, each);
		}
	}
	
	private void handleMethodAnnotations(ClassWriter javaWriter, Method method) {
		for (Annotation each : method.getAnnotations()) {
			if (getAnnotationType().isInstance(each)) {
				injectMethodWithCode(javaWriter, method, each);
			}
		}
	}
	
	protected abstract Class<? extends Annotation> getAnnotationType();
	protected abstract void injectMethodWithCode(ClassWriter javaWriter, Method method, Annotation each);
	
	protected String callSuperMethod(Method method) {
		String methodBody = method.getReturnType().equals(Void.TYPE)? 
				"    super." + method.getName() + "($$);": 
				"    return super." + method.getName() + "($$);";
		return methodBody;
	}
}
